package com.example.jiradata.service;

import com.example.jiradata.jpa.JiraIssuePriorityRepository;
import com.example.jiradata.pojo.JiraIssuePriority;
import com.example.jiradata.utils.HttpClientUtils;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.List;

@Service
@Transactional
public class JiraPrioritiesService {

    @Autowired
    private HttpClientUtils httpClientUtils;

    @Autowired
    ObjectMapper objectMapper;

    @Autowired
    private JiraIssuePriorityRepository jiraIssuePriorityRepository;

    public void syncPriority() throws IOException {
        String res = httpClientUtils.exchange("https://tngasia.atlassian.net/rest/api/2/priority",new ParameterizedTypeReference<String>(){},null, HttpMethod.GET);
        System.out.println(res);

        List<JiraIssuePriority> jiraPriorities = objectMapper.readValue(res, new TypeReference<List<JiraIssuePriority>>() {});
        for (JiraIssuePriority jiraP : jiraPriorities) {
            System.out.println(jiraP.getJiraPriorityId());
            jiraIssuePriorityRepository.save(jiraP);
        }
    }

}
